package GestionBilleterie;

import GestionPlannification.Representation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Classe permettant d'accéder aux Reservations de la base de données
 * @author Julien Reze
 */
class ReservationBD {
    /**
     * Ajout(insert) d'une Reservation dans la base de données
     * @param representationId
     * @param clientId
     * @param tarifId
     * @throws java.sql.SQLException
     */
    public static void ajoutReservation(int representationId,int clientId, int tarifId, int vendeurId) throws SQLException {
        Statement st = null;
        st=BD.UserBD.getStatement();
        String rqt;
        if(vendeurId==0)
            rqt="INSERT INTO reservation(repr_id,clie_id,tari_id,vend_id) VALUES("+representationId+","+clientId+","+tarifId+",null)";
        else
            rqt="INSERT INTO reservation(repr_id,clie_id,tari_id,vend_id) VALUES("+representationId+","+clientId+","+tarifId+","+vendeurId+")";
        st.executeUpdate(rqt);
        BD.UserBD.close(st);
    }

    /**
     * Recherche d'une Reservation dans la base de données grâce à son id
     * @param idReservation
     * @return la Reservation trouvée
     * @throws java.sql.SQLException
     */
    static Reservation readReservation(int idReservation) throws SQLException {
        Statement st = BD.UserBD.getStatement();
        ResultSet rs= st.executeQuery("SELECT * FROM reservation WHERE repr_id="+idReservation+"");
        rs.next();
        Reservation r=new Reservation(rs.getInt("rese_id"),
                Client.findEntitie(rs.getInt("clie_id")),
                Tarif.findEntitie(rs.getInt("clie_id")),
                Representation.findEntitie(rs.getInt("repr_id")),
                Vendeur.findEntitie(rs.getInt("vend_id")));
        BD.UserBD.close(st);
        return r;
    }

    /**
     * Retourne les Reservations de la base de données
     * @return la liste des Reservations
     * @throws java.sql.SQLException
     */
    static List<Reservation> readAllReservations() throws SQLException {
        Statement st = BD.UserBD.getStatement();
        List<Reservation> lesReservations = new ArrayList();
        ResultSet rs;
        rs = st.executeQuery("SELECT * FROM reservation");
        while (rs.next()) {
            Reservation r=new Reservation(rs.getInt("rese_id"),
                Client.findEntitie(rs.getInt("clie_id")),
                Tarif.findEntitie(rs.getInt("clie_id")),
                Representation.findEntitie(rs.getInt("repr_id")),
                Vendeur.findEntitie(rs.getInt("vend_id")));
            lesReservations.add(r);
        }
        BD.UserBD.close(st);
        return lesReservations;
    }

    /**
     * Recherche des reservations dans la base de données grâce à une Representation
     * @param representation
     * @return la liste des Reservations trouvées
     * @throws java.sql.SQLException
     */
    static List<Reservation> realAllReservations(Representation representation) throws SQLException {
        Statement st = BD.UserBD.getStatement();
        List<Reservation> lesReservations = new ArrayList();
        ResultSet rs;
        rs = st.executeQuery("SELECT * FROM reservation where repr_id="+representation.getId()+"");
        while (rs.next()) { 
            Reservation r=new Reservation(rs.getInt("rese_id"),
                Client.findEntitie(rs.getInt("clie_id")),
                Tarif.findEntitie(rs.getInt("tari_id")),
                representation,
                Vendeur.findEntitie(rs.getInt("vend_id")));
            lesReservations.add(r);
        }
        BD.UserBD.close(st);
        return lesReservations;
    }

}
